// pages/more/more.js
const app = getApp()
//微信同声传译
const plugin = requirePlugin('WechatSI');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    image: '',
    description: '',
    filename:"",
    src:""
  },

  onReady: function () {
    //创建内部 audio 上下文 InnerAudioContext 对象。
    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.onError(function (res) {
      console.log(res);
      wx.showToast({
        title: '语音播放失败',
        icon: 'none',
      })
    })
  },
  change:function(){
    var that = this;
    that.robot()
  },
  // 文字转语音
  start: function (e) {
    var that = this;
    plugin.textToSpeech({
      lang: "zh_CN",
      tts: true,
      content: this.data.description,
      success: function (res) {
        console.log(res);
        console.log("succ tts", res.filename);
        that.setData({
          src: res.filename
        })
        // 播报语音
        that.yuyinPlay();
      },
      fail: function (res) {
        console.log("fail tts", res)
      }
    });
  },
  
  //播放语音
  yuyinPlay: function (e) {
    if (this.data.src == '') {
      console.log("暂无语音");
      return;
    }
    var that = this;
    console.log('this',this.data.src);
    console.log(that.innerAudioContext.src);
    // console.log(this.innerAudioContext.src);
    // var voicePath = this.data.src
    // this.innerAudioContext.src = voicePath;   //设置音频地址
    // this.innerAudioContext.src = this.data.src 
    // this.innerAudioContext.play(); //播放音频
  },
 
  // 结束语音
  end: function (e) {
    this.innerAudioContext.pause();//暂停音频
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    var that = this;
    that.setData({
      title: options.title,
      image: options.image,
      description: options.description,
    })
    console.log(that.data.description);
    if (that.data.description === 'undefined') {
      that.setData({
        image: '../../images/plant.jpg',
        description:'暂时没有相关信息噢~~~'
      })
    }
    console.log('LATER',that.data.description);
},


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})